<style>

.uploader{    
	border-radius: 5px;
    border: 1px solid #ddd;
    width: 100%;
    margin-bottom: 5px;
}
.uploader_body{
	width:100%;
	position: relative;
	background: #eee;
}
.uploader input[type="file"]{
	display: none
}

.uploader_item{
	height:200px;
	width:200px;
	background-color: #fff;
	margin:5px;
	float: left;
	border: 1px #ccc solid;
	position:relative;
	box-sizing: content-box;
	border-radius: 4px;
	overflow: hidden;
	background-size: contain;
	background-repeat: no-repeat;
	background-position: center
}
.uploader_item>i{
	font-style: normal;
	font-weight: bold;
	font-size: 16px;
	line-height: 180px;
	text-align: center
}
.uploader_progress{
	position: absolute;
	top:0;
	width:100%;
    left:0;
}

#theme_viewer{
	width:200px;height:200px;border-radius: 3px;border:1px #ccc solid;background-position: center;background-repeat: no-repeat;background-size: contain;
}


.attr_body{border-radius: 5px;border: 1px solid #ddd;width: 100%;margin-bottom: 5px;}
.attr_content{width:100%;position: relative;background: #eee;}
.attr_item{}
.attr_item_title{position: relative;}
.attr_btn_group{position:absolute;right:15px;top:5px;}
.control-label{text-align: right}
.help-block{padding-left: 15px}
</style>

<form method="post" class="form-horizontal" id="product_form">
<?php if(isset($product)):?><input type="hidden" id="form_id" name="id" value="<?=$product['id']?>"><?php endif;?> 
		<div class="form-group">
		    <label class="col-sm-2 control-label" for="form_host_id">域名</label>
		    <div class="col-sm-10">
			    <select class="form-control" name="host_id" id="form_host_id">
				<?php foreach ($host as $item):?>
				  <option value="<?=$item['id']?>" <?php echo isset($product)&&$product['host_id']==$item['id']?'selected':'';?>><?=$item['name']?></option>
				<?php endforeach;?>
				</select>
			</div>
		</div>
		
		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_uri">二级域名</label>
			<div class="col-sm-10">
				<div class="input-group">
					<input type="text" class="form-control" name="uri" id="form_uri" value="<?php echo isset($product)?$product['uri']:'';?>">
					<span class="input-group-addon">.html</span>
				</div>
			</div>
		</div>

		<div class="form-group">
		    <label class="col-sm-2 control-label" for="form_location_id">地区</label>
		    <div class="col-sm-10">
			    <select class="form-control" id="form_location_id" name="location_id">
				  	<?php foreach ($location as $item):?>
					  <option value="<?=$item['id']?>" <?php echo isset($product)&&$product['location_id']==$item['id']?'selected':'';?>><?=$item['name']?></option>
					<?php endforeach;?>
				</select>
			</div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_name">商品中文名称</label>
			<div class="col-sm-10">
		    	<input type="text" class="form-control" name="name" id="form_name" value="<?php echo isset($product)?$product['name']:'';?>">
		    </div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_title">标题</label>
			<div class="col-sm-10">
		    	<input type="text" class="form-control" name="title" id="form_title" value="<?php echo isset($product)?$product['title']:'';?>">
		    </div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_fb_id">FB-ID</label>
			<div class="col-sm-10">
		    	<input type="text" class="form-control" name="fb_id" id="form_fb_id" value="<?php echo isset($product)?$product['fb_id']:'';?>">
		    </div>
		    <span class="help-block col-sm-offset-2">多个ID以“,”隔开</span>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_seo_keywords">SEO关键词</label>
			<div class="col-sm-10">
		    	<input type="text" class="form-control" name="seo_keywords" id="form_seo_keywords" value="<?php echo isset($product)?$product['seo_keywords']:'';?>">
		    </div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_seo_descript">SEO描述</label>
			<div class="col-sm-10">
				<textarea class="form-control" name="seo_descript" id="form_seo_descript" rows="2"><?php echo isset($product)?$product['seo_descript']:'';?></textarea>
		    </div>
		</div>

		

		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_price_base">原价</label>
			<div class="col-sm-5">
		    	<input type="number" class="form-control" id="form_price_base" name="price_base" value="<?php echo isset($product)?$product['price_base']:0;?>">
		    </div>
		    <div class="col-sm-5"></div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_price">售价</label>
			<div class="col-sm-5">
			    <input type="number" id="form_price" name="price" class="form-control" value="<?php echo isset($product)?$product['price']:0;?>">
			</div>
			<div class="col-sm-5"></div>
		</div>

		
		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_theme">模板</label>
			<div class="col-sm-5">
				<select class="form-control" name="theme" id="form_theme">
				<?php foreach ($theme as $item):?>
				  <option value="<?=$item['path']?>" <?php echo isset($product)&&$product['theme']==$item['path']?'selected':'';?>><?=$item['title']?></option>
				<?php endforeach;?>
				</select>
				<div id="theme_viewer"></div>
			</div>
			<div class="col-sm-5"></div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label">缩略图</label>
			<div class="col-sm-5">
				<input type="text" style="display: none" id="form_thumb" name="thumb" value='<?php echo isset($product)?$product['thumb']:'';?>'>
			</div>
		</div>
		
		<div class="form-group">
			<label class="col-sm-2 control-label">展示图</label>
			<div class="col-sm-5">
				<input type="text" style="display: none" id="form_img_banners" name="img_banners" value='<?php echo isset($product)?$product['img_banners']:'';?>'>
			</div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label">套餐</label>
			<div class="col-sm-10">
				<input type="text" style="display: none" id="form_suit" name="suit" value='<?php echo isset($product)?$product['suit']:'[{"name":"儿童","count":"1","price_add":"0"},{"name":"泰语","count":"1","price_add":"0"}]';?>'>
			</div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label">属性</label>
			<div class="col-sm-10">
				<input type="text" style="display: none" id="form_attr" name="attr" value='<?php echo isset($product)?$product['attr']:'[]';?>'>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_is_message">订单通知</label>
			<div class="col-sm-10">
				<div class="btn-group" data-toggle="buttons">
				  <label class="btn btn-primary <?php echo !isset($product)||(isset($product)&&$product['order_message']==0)?'active':'';?>">
				    <input type="radio" name="order_message" value="0" autocomplete="off"  <?php echo !isset($product)||(isset($product)&&$product['order_message']==0)?'checked':'';?>>不通知
				  </label>
				  <label class="btn btn-primary <?php echo isset($product)&&$product['order_message']==1?'active':'';?>">
				    <input type="radio" name="order_message" value="1" autocomplete="off" <?php echo isset($product)&&$product['order_message']==1?'checked':'';?>>短信
				  </label>
				  <label class="btn btn-primary <?php echo isset($product)&&$product['order_message']==2?'active':'';?>">
				    <input type="radio" name="order_message" value="2" autocomplete="off" <?php echo isset($product)&&$product['order_message']==2?'checked':'';?>>邮件
				  </label>
				  <label class="btn btn-primary <?php echo isset($product)&&$product['order_message']==3?'active':'';?>">
				    <input type="radio" name="order_message" value="3" autocomplete="off" <?php echo isset($product)&&$product['order_message']==3?'checked':'';?>>短信和邮件
				  </label>
				</div>
		    </div>
		</div>
		<div class="form-group">
	  		<label class="col-sm-2 control-label" for="form_tip">购买提示</label>
	  		<div class="col-sm-10">
	  			<textarea class="form-control" name="tip" id="form_tip" rows="2"><?php echo isset($product)?$product['tip']:'';?></textarea>
	  	    </div>
	  	</div>

		<div class="form-group">
			<label class="col-sm-2 control-label" for="form_water_mark">水印文字</label>
			<div class="col-sm-5">
				<div class="input-group">
					<input type="text" class="form-control" name="water_mark" id="form_water_mark" value="<?php echo isset($product)?$product['water_mark']:'';?>">
					<span class="input-group-btn"><button class="btn btn-primary" id="btn_water" type="button">使用产品链接</button></span>
				</div>
		    </div>
		    <span class="help-block">修改水印文字只对新上传的图片有效</span>
		</div>

		<div class="form-group">
	  		<label class="col-sm-2 control-label" for="form_content">商品详情</label>
	  		<div class="col-sm-10">
	  	    	<textarea id="form_content" name="content" rows="5"><?php echo isset($product)?$product['content']:'';?></textarea>
	  	    </div>
	  	</div>
	  	<div class="form-group">
	  		<label class="col-sm-2 control-label"></label>
	  		<div class="col-sm-10">
	  	    	<button type="button" id="btn_submit" data-loading-text="正在提交..." class="btn btn-primary">提交</button>
	  	    </div>
	  	</div>


		
		
</form>
<script type="text/javascript">

$.fn.fileUpload = function(upload_url){
	var upload_main = $('<div class="uploader"></div>');
	var upload_body = $('<div class="clearfix uploader_body"></div>');
	var upload_btn = $('<button class="btn btn-primary uploader_btn" type="button"><span class="glyphicon glyphicon-picture" aria-hidden="true"></span>  选择</button>');
	var upload_selecter = $('<input type="file" title="选择图片" accept="image/jpeg,image/png"/>');
	upload_main.append(upload_btn).append(upload_body).append(upload_selecter);
	$(this).after(upload_main);
	upload_btn.click(function(){
		upload_selecter.click();
	});
	var main_input = $(this);
	var setValue = function(){
		var value = upload_body.children('.uploader_item').data('url');
		main_input.val(value);
	}
	var add_image = function(url){
		if(url=='') return;
		var imgItem = $('<div class="uploader_item" data-url="'+url+'" style="background-image:url('+url+')"></div>');
		upload_body.html(imgItem);
	}
	add_image($(this).val());
	upload_selecter.change(function(){
		var xhrOnProgress=function(fun) {
	  		xhrOnProgress.onprogress = fun;
	  		return function() {
		    	var xhr = $.ajaxSettings.xhr();
		    	if(typeof xhrOnProgress.onprogress !== 'function') return xhr;
		    	if(xhrOnProgress.onprogress && xhr.upload){
		      		xhr.upload.onprogress = xhrOnProgress.onprogress;
		    	}
		    	return xhr;
		  	}
		};
		var imgItem = $('<div class="uploader_item"><i class="text-primary center-block">正在上传...</i><div class="progress uploader_progress"><div class="progress-bar" style="width: 0%;"></div></div></div>');
		upload_body.html(imgItem);
		var data = new FormData();
		data.append('upload_file',this.files[0]);
		if(typeof(ie)=='undefined') {
        	$(this).val('');
		} else {
		    $(this).replaceWith($(this).clone(true));
		}
		$.ajax({
            url:upload_url,
            type:'POST',
            data:data,
            cache: false,
            contentType: false,
            processData: false,
            success:function(data){
            	if(data.statu=='ok'){
            		add_image(data.result.path);
            	}else{
            		imgItem.children('i').html('<span class="text-danger">'+data.message+'</span>');
            	}
            	setValue();
            },
            error:function(){
            	imgItem.children('i').html('<span class="text-danger">上传失败！</span>');
            	setValue();
            },
            xhr:xhrOnProgress(function(e){
            	var progrss = imgItem.find('.progress-bar');
			    var percent=parseInt(e.loaded*100/e.total)+'%';
			    progrss.css('width',percent);
			})
        });
        
	})
}

$.fn.proSuit = function(){
	var main_input = $(this);
	var attr_body = $('<div class="attr_body"></div>');
	var attr_add_btn = $('<button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-pushpin" aria-hidden="true"></span>  添加</button>');
	var attr_content = $('<div class="attr_content"></div>');
	attr_body.append(attr_add_btn).append(attr_content);
	var table = $('<table class="table" style="margin:0"></table>');
	var table_head = $('<thead><tr><th>套餐名称</th><th width="100">产品数量</th><th width="100">差价</th><th width="100">操作</th></tr></thead>');
	var table_body = $('<tbody></tbody>');
	table.append(table_head).append(table_body);
	attr_content.append(table);
	$(this).after(attr_body);
	attr_add_btn.click(function(){
		if(table_body.children('tr').length>=8){
			bs_alert('最多添加8个套餐');
		    return;
		}
		mkForm();
	});
	var setVal = function(){
		var vals = [];
		table_body.children('tr').each(function(){
			vals.push($(this).data('it'));
		});
		main_input.val(JSON.stringify(vals));
	}

	var doItem = function(result,$obj){
		if($obj){
			var tds = $obj.children('td');
	      	$obj.data('it',result);
	      	tds.eq(0).text(result.name);
	      	tds.eq(1).text(result.count);
	      	tds.eq(2).text(result.price_add);
	      }else{
	      	var btn_group = $('<div class="btn-group btn-group-sm"></div>');
    		var btn_edit = $('<button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-edit"></span></button>');
    		var btn_dele = $('<button type="button" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span></button>');
	      	var item = $('<tr><td>'+result.name+'</td><td>'+result.count+'</td><td>'+result.price_add+'</td></tr>');
    		var td = $('<td></td>');
    		btn_group.append(btn_edit).append(btn_dele);
    		td.append(btn_group);
    		item.append(td).data('it',result);
    		table_body.append(item);
    		btn_edit.click(function(){
    			mkForm(result,item)
    		})
    		btn_dele.click(function(){
    			item.remove();
    			setVal();
    		})
	      }
	}

	var mkForm = function(config,$obj){
		var title = config?'编辑套餐':'添加套餐';
		var data = config?config:{name:'',count:1,price_add:0};
		var html = '<form id="location_form">'+makeInput({name:'name',label:'套餐名称',value:data.name,type:'text'})+makeInput({name:'count',label:'产品数量',value:data.count,type:'number'})+makeInput({name:'price_add',label:'差价',value:data.price_add,type:'number'})+'</form>';
		var $input = $(this);
		var win = layer.open({
		    title:title,
		    area: ['520px'], //宽高
		    content: html,
		    btn:['提交','取消'],
		    yes:function(index){
		      var vals = $("#location_form").serializeArray();
		      var result = {};
		      $.each(vals,function(k,v){
		      	result[v.name] = $.trim(v.value);
		      })
		      if(result.name.length<1||result.name.length>30){
		      	layer.msg('套餐名称为1-10个字符！', {icon: 2});
	    		return;
		      }
		      if(result.count<1||result.count>5){
		      	layer.msg('产品数量为1-5！', {icon: 2});
	    		return;
		      }
		      if(result.price_add<0||result.price_add>100000){
		      	layer.msg('差价为1-100000！', {icon: 2});
	    		return;
		      }
		      layer.close(win);
		      if($obj){
		      	doItem(result,$obj);
		      }else{
		      	doItem(result);
		      }
		      setVal();
		    }
		});
	}
	var attrArry = JSON.parse(main_input.val());
	$.each(attrArry,function(key,attrObj){
		doItem(attrObj)
	});
}

$.fn.attrParse = function(){
	var main_input = $(this);
	var attr_body = $('<div class="attr_body"></div>')
	var attr_add_btn = $('<button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-pushpin" aria-hidden="true"></span>  添加</button>');
	var attr_content = $('<div class="attr_content"></div>');
	attr_body.append(attr_add_btn).append(attr_content);
	$(this).after(attr_body);
	
	attr_add_btn.click(function(){
		if(attr_content.children('div.attr_item').length>=4){
			layer.msg('最多只能添加4个属性！', {icon: 2});
		    return;
		}
		var prompt = layer.prompt({title: '请输入属性名称', formType: 0,value:name}, function(text, index){
		    text = $.trim(text);
		    if(text.length<1||text.length>10){
		    	layer.msg('请输入1-10个字符！', {icon: 2});
		    	return;
		    }
		    layer.close(prompt);
		    addAttr(text);
		    dom2val();
		});
	});

	var addAttr = function(name){
		var item_box = $('<div class="panel panel-default attr_item" style="margin:0;border:none" data-name="'+name+'"></div>');
	    var item_title = $('<div class="panel-heading attr_item_title">'+name+'</div>');
	    var btn_group = $('<div class="btn-group btn-group-sm attr_btn_group"></div>');
	    var btn_add = $('<button type="button" class="btn btn-success">添加值</button>');
	    var btn_dele = $('<button type="button" class="btn btn-danger">删除属性</button>');
	    var table = $('<table class="table"></table>');
	    var table_head = $('<thead><tr><th>值</th><th width="100">差价</th><th width="180">操作</th></tr></thead>');
	    var table_body = $('<tbody></tbody>');
	    btn_group.append(btn_add).append(btn_dele);
	    item_title.append(btn_group);
	    table.append(table_head).append(table_body);
	    item_box.append(item_title).append(table)
	    attr_content.append(item_box);
	    btn_add.click(function(){
	    	var html = '<form id="location_form">'+makeInput({name:'value',label:'值',value:'',type:'text'})+makeInput({name:'price_add',label:'差价',value:'0',type:'number'})+'</form>';
			layer.open({
			    title:'新增'+name,
			    area: ['520px'], //宽高
			    content: html,
			    btn:['提交','取消'],
			    yes:function(index){
			      var data = $("#location_form").serializeArray();
			      var result = {};
			      $.each(data,function(k,v){
			      	result[v.name] = $.trim(v.value);
			      })
			      if(result.value.length<1||result.value.length>30){
			      	layer.msg('值为1-10个字符！', {icon: 2});
		    		return;
			      }
			      if(result.price_add<0||result.price_add>100000){
			      	layer.msg('差价为1-100000！', {icon: 2});
		    		return;
			      }
			      layer.close(index);
			      addItem(table_body,result);
			      dom2val();
			    }
			});
	    })
	    btn_dele.click(function(){
	    	var confirm = layer.confirm('确定删除？', 
	    	{btn: ['确定','取消']}, 
	    	function(){
	    		item_box.remove();
			    layer.close(confirm);
			    dom2val();
			});
	    });
	    
	    return item_box;
	}

	var addItem = function(tbody,data){
		var btn_group = $('<div class="btn-group btn-group-sm"></div>');
	    var btn_edit = $('<button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-edit"></span></button>');
	    var btn_dele = $('<button type="button" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span></button>');
	    var item = $('<tr data-value="'+data.value+'" data-price-add="'+data.price_add+'"><td>'+data.value+'</td><td>'+data.price_add+'</td></tr>');
	    var td = $('<td></td>');
	    btn_group.append(btn_edit).append(btn_dele);
	    td.append(btn_group);
	    item.append(td);
		tbody.append(item);
		btn_edit.click(function(){
			var html = '<form id="location_form">'+makeInput({name:'value',label:'值',value:item.data('value'),type:'text'})+makeInput({name:'price_add',label:'差价',value:item.data('price-add'),type:'number'})+'</form>';
			layer.open({
			    title:'编辑-'+name,
			    area: ['520px'], //宽高
			    content: html,
			    btn:['提交','取消'],
			    yes:function(index){
			      var data = $("#location_form").serializeArray();
			      var result = {};
			      $.each(data,function(k,v){
			      	result[v.name] = $.trim(v.value);
			      })
			      if(result.value.length<1||result.value.length>30){
			      	layer.msg('值为1-10个字符！', {icon: 2});
		    		return;
			      }
			      if(result.price_add<0||result.price_add>100000){
			      	layer.msg('差价为1-100000！', {icon: 2});
		    		return;
			      }
			      layer.close(index);
			      item.data('value',result.value).data('price-add',result.price_add);
			      var tds = item.children();
			      tds.eq(0).html(result.value);
			      tds.eq(1).text(result.price_add);
			      dom2val();
			    }
			});
		});
		btn_dele.click(function(){
			item.remove();
			dom2val();
		})
		return item;
	}
	var dom2val = function(){
		var attrArry = [];
		attr_content.children('div.attr_item').each(function(){
			var attrObj = {};
			attrObj.name = $(this).data('name');
			attrObj.option = [];
			$(this).find('tbody').children().each(function(){
				var option = {};
				option.value = $(this).data('value');
				option.price_add = $(this).data('price-add');
				attrObj.option.push(option);
			})
			attrArry.push(attrObj); 
		})
		main_input.val(JSON.stringify(attrArry));
	}
	var attrArry = JSON.parse(main_input.val());
	$.each(attrArry,function(key,attrObj){
		var item_box = addAttr(attrObj.name);
		$.each(attrObj.option,function(k,v){
			addItem(item_box.find('tbody'),v);
		});
	});
}

function makeInput(config){//type value name label
  var inputStr = '<div class="form-group">';
  inputStr+='<label class="col-sm-2 control-label" for="form_'+config.name+'">'+config.label+'</label>';
  inputStr+='<input type="'+config.type+'" class="form-control" name="'+config.name+'" id="form_'+config.name+'" value="'+config.value+'"></div>';
  return inputStr;
}



function themeView(name){
	$('#theme_viewer').css('background-image','url(/theme/'+name+'/thum.png)');
}

$(function(){
	$('#form_img_banners').fileUpload('/api/upload/image_product_shows.html');
	$('#form_thumb').fileUpload('/api/upload/image_product_shows.html');
	$("#form_attr").attrParse();
	$("#form_suit").proSuit();
	var ue = UE.getEditor('form_content',{
    	initialFrameWidth:'100%',
    	initialFrameHeight:600,
    	scaleEnabled:false
	});
	$('#form_price').blur(function(){
		var price_base = parseInt($('#form_price_base').val());
		var price = parseInt($(this).val());
		if(price_base<=0||price_base>=100000){
			$('#form_price_base').formTip('1-99999的数字！','error');
		}else{
			$('#form_price_base').formTip();
		}
		if(price<=0||price>=100000){
			$(this).formTip('1-99999的数字！','error');
		}else if(price>price_base){
			$(this).formTip('售价应小于或等于原价！','error');
		}else{
			if(price==price_base){
				$(this).formTip();
			}else{
				var discount = Math.round(price*100/price_base);
				$(this).formTip('折扣为'+discount+'%','success');
			}
		}
	})
	$('#form_price_base').blur(function(){
		var price_base = parseInt($(this).val());
		if(price_base<=0||price_base>=100000){
			$(this).formTip('1-99999的数字！','error');
		}else{
			$(this).formTip();
		}
	})
	$('#form_name').blur(function(){
		var value = $.trim($(this).val());
		if(value==''){
			$(this).formTip('不能为空！','error');
			return;
		}
		if(value.length>50){
			$(this).formTip('不能超过50个字符！','error');
			return;
		}
		$(this).formTip();
	})
	
	$('#form_water_mark').blur(function(){
		var value = $.trim($(this).val());
		if(value.length>50){
			$(this).formTip('不能超过50个字符！','error');
			return;
		}
		$(this).formTip();
	})
	$('#form_seo_keywords').blur(function(){
		var value = $.trim($(this).val());
		if(value.length>50){
			$(this).formTip('不能超过50个字符！','error');
			return;
		}
		$(this).formTip();
	})
	$('#form_seo_descript').blur(function(){
		var value = $.trim($(this).val());
		if(value.length>50){
			$(this).formTip('不能超过200个字符！','error');
			return;
		}
		$(this).formTip();
	})
	$('#form_title').blur(function(){
		var value = $.trim($(this).val());
		if(value==''){
			$(this).formTip('不能为空！','error');
			return;
		}
		if(value.length>50){
			$(this).formTip('不能超过50个字符！','error');
			return;
		}
		$(this).formTip();
	})
	$('#form_tip').blur(function(){
		var value = $.trim($(this).val());
		if(value.length>300){
			$(this).formTip('不能超过300个字符！','error');
			return;
		}
		$(this).formTip();
	})
	
	$('#btn_water').click(function(){
		if(!$('#form_uri').parents('.form-group').hasClass('has-success')&&$('#form_id').length!=1){
			layer.msg('请先验证二级域名！', {icon: 2});
			return;
		};
		var value = $("#form_host_id").find("option:selected").text()+'/'+$('#form_uri').val();
		$('#form_water_mark').val(value);
	})

	var theme_sele = $('#form_theme');
	themeView(theme_sele.val())
	theme_sele.change(function(){
		themeView($(this).val())
	})

	$('#form_uri').blur(function(){
		if($.trim($(this).val())==''){
			$(this).formTip('不能为空！','error');
			return;
		}
		$(this).formTip('正在查询...','warning');
		var postData = {host_id:$('#form_host_id').val(),uri:$.trim($(this).val())};
		var idInput = $('#form_id');
		if(idInput.length==1) postData.id=idInput.val();
		var $input = $(this);
		$.post('<?php echo site_url("admin/product/checkurl");?>',postData,function(data){
			if(data.statu=='ok'){
				$input.formTip('链接可以使用！','success');
			}else{
				$input.formTip(data.message,'error');
			}
		})
	})
	
	$('#btn_submit').click(function(){
		var $form = $('#product_form');
		if($form.children('.has-error').length>0){
			layer.msg('请检查表单！', {icon: 2});
			return;
		}
		var btn_sm = $(this);
		btn_sm.button('loading');
		$.post('<?php echo site_url("admin/product/ajax_form");?>',$form.serializeArray(),function(data){
			btn_sm.button('reset');
			if(data.statu=='ok'){
				location.href = '<?php echo site_url("admin/product");?>';
			}else{
				layer.msg(data.message, {icon: 2});
			}
		})
	})
})

</script>
